package com.zdxlz.fcmp.upms.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdxlz.fcmp.common.core.constant.enums.OperateTypeEnum;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LogRecord;
import com.zdxlz.fcmp.upms.api.dto.SystemTenantDto;
import com.zdxlz.fcmp.upms.api.entity.SystemTenant;
import com.zdxlz.fcmp.upms.api.service.ISystemTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * 租户信息表(SystemTenant)表控制层
 *
 * @author aman
 * @since 2025-02-24 10:54:59
 */
@RestController
@Tag(name = "租户管理模块")
@LogRecord(module = "租户管理模块")
@RequestMapping("/api/v1/sys/tenant")
public class SystemTenantController {

    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    /**
     * 服务对象
     */
    @Resource
    private ISystemTenantService systemTenantService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param dto  查询实体
     * @return 所有数据
     */
    @GetMapping
    @Operation(summary = "分页查询租户")
    @LogRecord(module = "分页查询租户", value = "查询条件，租户名称【{}】，手机号【{}】，租户状态【{}】", field = {"name", "contactMobile", "status"}, operType = OperateTypeEnum.SELECT)
    public R selectSystemTenant(@ParameterObject Page<SystemTenant> page, @ParameterObject SystemTenantDto dto) {
        return R.ok(systemTenantService.selectSystemTenant(page, dto), OPERATION_SUCCESS);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询单个租户")
    @LogRecord(module = "查询单个租户", value = "查询条件，租户ID【{}】", field = {"id"}, operType = OperateTypeEnum.SELECT)
    public R getSystemTenantById(@PathVariable Long id) {
        return R.ok(systemTenantService.getById(id), OPERATION_SUCCESS);
    }

    /**
     * 新增数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增租户")
    @LogRecord(module = "新增租户", value = "新增租户必要条件，租户名称【{}】，租户管理员名称：【{}】，租户手机号：【{}】", field = {"name", "contactName", "contactMobile"}, db = true, operType = OperateTypeEnum.INSERT)
    public R insertSystemTenant(@RequestBody @Validated SystemTenant entity) {
        return R.ok(systemTenantService.insertSystemTenant(entity, ENCODER.encode(DEFAULT_PASSWORD)), CREATE_SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新租户")
    @LogRecord(module = "更新租户", value = "更新租户必要条件，租户ID【{}】，租户名称【{}】，租户管理员名称：【{}】，租户手机号：【{}】", field = {"id", "name", "contactName", "contactMobile"}, db = true, operType = OperateTypeEnum.UPDATE)
    public R updateSystemTenant(@PathVariable Integer id, @RequestBody @Validated SystemTenant entity) {
        entity.setId(id);
        return R.ok(systemTenantService.updateSystemTenant(entity), UPDATE_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @Operation(summary = "删除租户")
    @LogRecord(module = "删除租户", value = "删除租户必要条件，租户ID【{}】", field = {"ids"}, db = true, operType = OperateTypeEnum.DELETE)
    public R deleteSystemTenant(@RequestBody List<Integer> ids) {
        return R.ok(systemTenantService.deleteSystemTenant(ids), DELETE_SUCCESS);
    }
}
